home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / gs262 / gsdevice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-29  |  18.9 KB  |  667 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsdevice.c */
  20. /* Device operators for Ghostscript library */
  21. #include "math_.h"            /* for fabs */
  22. #include "memory_.h"            /* for memcpy */
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsprops.h"
  26. #include "gsutil.h"
  27. #include "gxarith.h"
  28. #include "gzstate.h"
  29. #include "gzdevice.h"
  30. #include "gxdevmem.h"
  31.  
  32. #ifdef AMIGA
  33. extern void gx_set_cmap_procs(gs_state *);
  34. extern int gs_initmatrix(gs_state *);
  35. extern int gs_initclip(gs_state *);
  36. extern int gs_erasepage(gs_state *);
  37. extern int gx_remap_color(gs_state *);
  38. #endif
  39.  
  40. /* Import the device list from gdevs.c */
  41. extern gx_device *gx_device_list[];
  42.  
  43. /* Device definitions */
  44. /* Following defines the null device */
  45. private dev_proc_fill_rectangle(null_fill_rectangle);
  46. private dev_proc_copy_mono(null_copy_mono);
  47. private dev_proc_get_xfont_procs(null_get_xfont_procs);
  48. private dev_proc_get_xfont_device(null_get_xfont_device);
  49.  
  50. /* The null device procedure record is also used to fill in */
  51. /* NULL procedures in actual devices, so it must be complete. */
  52. private gx_device_procs null_procs = {
  53.     gx_default_open_device,
  54.     gx_default_get_initial_matrix,
  55.     gx_default_sync_output,
  56.     gx_default_output_page,
  57.     gx_default_close_device,
  58.     gx_default_map_rgb_color,
  59.     gx_default_map_color_rgb,
  60.     null_fill_rectangle,
  61.     gx_default_tile_rectangle,
  62.     null_copy_mono,
  63.     gx_default_copy_color,
  64.     gx_default_draw_line,
  65.     gx_default_get_bits,
  66.     gx_default_get_props,
  67.     gx_default_put_props,
  68.     gx_default_map_cmyk_color,
  69.     null_get_xfont_procs,
  70.     null_get_xfont_device
  71. };
  72. gx_device_null gs_null_device = {
  73.     sizeof(gx_device),
  74.     &null_procs,
  75.     "null",
  76.     0, 0,
  77.     72, 72,
  78.     no_margins,
  79.     dci_black_and_white,
  80.     1,
  81.     0                /* target */
  82. };
  83.  
  84. /* Fill in a single procedure */
  85. #define fill_default(procs, p, dproc)\
  86.   if ( (procs)->p == 0 ) (procs)->p = dproc
  87. #define fill_proc(procs, p)\
  88.   fill_default(procs, p, null_procs.p)
  89.  
  90. /* Fill in NULL procedures in a device procedure record. */
  91. void
  92. gx_device_procs_complete(register gx_device_procs *procs)
  93. {    fill_proc(procs, open_device);
  94.     fill_proc(procs, get_initial_matrix);
  95.     fill_proc(procs, sync_output);
  96.     fill_proc(procs, output_page);
  97.     fill_proc(procs, close_device);
  98.     fill_proc(procs, map_rgb_color);
  99.     fill_proc(procs, map_color_rgb);
  100.     /* NOT fill_rectangle */
  101.     fill_proc(procs, tile_rectangle);
  102.     /* NOT copy_mono */
  103.     fill_proc(procs, copy_color);        /* Bogus? */
  104.     fill_proc(procs, draw_line);
  105.     fill_proc(procs, get_bits);
  106.     fill_proc(procs, get_props);
  107.     fill_proc(procs, put_props);
  108.     fill_proc(procs, map_cmyk_color);
  109.     fill_default(procs, get_xfont_procs, gx_default_get_xfont_procs);
  110.     fill_default(procs, get_xfont_device, gx_default_get_xfont_device);
  111. }
  112. void
  113. gx_device_complete_procs(gx_device *dev)
  114. {    gx_device_procs_complete(dev->procs);
  115. }
  116.  
  117. /* Flush buffered output to the device */
  118. int
  119. gs_flushpage(gs_state *pgs)
  120. {    gx_device *dev = pgs->device->info;
  121.     return (*dev->procs->sync_output)(dev);
  122. }
  123.  
  124. /* Make the device output the accumulated page description */
  125. int
  126. gs_copypage(gs_state *pgs)
  127. {    return gs_output_page(pgs, 1, 0);
  128. }
  129. int
  130. gs_output_page(gs_state *pgs, int num_copies, int flush)
  131. {    gx_device *dev = pgs->device->info;
  132.     return (*dev->procs->output_page)(dev, num_copies, flush);
  133. }
  134.  
  135. /* Copy scan lines from an image device */
  136. int
  137. gs_copyscanlines(gx_device *dev, int start_y, byte *data, uint size,
  138.   int *plines_copied, uint *pbytes_copied)
  139. {    uint line_size = gx_device_raster(dev, 0);
  140.     uint count = size / line_size;
  141.     uint i;
  142.     byte *dest = data;
  143.     for ( i = 0; i < count; i++, dest += line_size )
  144.     {    int code = (*dev->procs->get_bits)(dev, start_y + i, dest, NULL);
  145.         if ( code < 0 )
  146.         {    /* Might just be an overrun. */
  147.             if ( start_y + i == dev->height ) break;
  148.             return_error(code);
  149.         }
  150.     }
  151.     if ( plines_copied != NULL )
  152.       *plines_copied = i;
  153.     if ( pbytes_copied != NULL )
  154.       *pbytes_copied = i * line_size;
  155.     return 0;
  156. }
  157.  
  158. /* Get the current device from the graphics state */
  159. gx_device *
  160. gs_currentdevice(const gs_state *pgs)
  161. {    return pgs->device->info;
  162. }
  163.  
  164. /* Get the name of a device */
  165. const char *
  166. gs_devicename(const gx_device *dev)
  167. {    return dev->dname;
  168. }
  169.  
  170. /* Get the initial matrix of a device. */
  171. void
  172. gs_deviceinitialmatrix(gx_device *dev, gs_matrix *pmat)
  173. {    fill_proc(dev->procs, get_initial_matrix);
  174.     (*dev->procs->get_initial_matrix)(dev, pmat);
  175. }
  176.  
  177. /* Get the N'th device from the known device list */
  178. gx_device *
  179. gs_getdevice(int index)
  180. {    int i;
  181.     for ( i = 0; gx_device_list[i] != 0; i++ )
  182.        {    if ( i == index ) return gx_device_list[i];
  183.        }
  184.     return 0;            /* index out of range */
  185. }
  186.  
  187. /* Clone an existing device. */
  188. int
  189. gs_copydevice(gx_device **pnew_dev, const gx_device *dev, const gs_memory_procs *mprocs)
  190. {    register gx_device *new_dev;
  191.     new_dev = (gx_device *)(*mprocs->alloc)(1, dev->params_size, "gs_copydevice");
  192.     if ( new_dev == 0 ) return_error(gs_error_VMerror);
  193.     memcpy(new_dev, dev, dev->params_size);
  194.     new_dev->is_open = 0;
  195.     *pnew_dev = new_dev;
  196.     return 0;
  197. }
  198.  
  199. /* Make a memory (image) device. */
  200. /* If num_colors = -16, -24, or -32, this is a true-color device; */
  201. /* otherwise, num_colors is the number of elements in the palette */
  202. /* (2^N or 3*2^N). */
  203. int
  204. gs_makeimagedevice(gx_device **pnew_dev, const gs_matrix *pmat,
  205.   uint width, uint height, const byte *colors, int num_colors,
  206.   const gs_memory_procs *mprocs)
  207. {    const gx_device_memory *old_dev;
  208.     register gx_device_memory *new_dev;
  209.     int palette_size = num_colors;
  210.     int bpp = 1;
  211.     int pcount;
  212.     int bits_per_pixel;
  213.     float x_pixels_per_unit, y_pixels_per_unit;
  214.     byte palette[256 * 3];
  215.     byte *dev_palette;
  216.     int has_color;
  217.     if ( width <= 0 || height <= 0 ) return_error(gs_error_rangecheck);
  218.     switch ( num_colors )
  219.        {
  220.     case 3*2:
  221.         palette_size = 2; bpp = 3;
  222.     case 2:
  223.         bits_per_pixel = 1; break;
  224.     case 3*4:
  225.         palette_size = 4; bpp = 3;
  226.     case 4:
  227.         bits_per_pixel = 2; break;
  228.     case 3*16:
  229.         palette_size = 16; bpp = 3;
  230.     case 16:
  231.         bits_per_pixel = 4; break;
  232.     case 3*256:
  233.         palette_size = 256; bpp = 3;
  234.     case 256:
  235.         bits_per_pixel = 8; break;
  236.     case -16:
  237.         bits_per_pixel = 16; palette_size = 0; break;
  238.     case -24:
  239.         bits_per_pixel = 24; palette_size = 0; break;
  240.     case -32:
  241.         bits_per_pixel = 32; palette_size = 0; break;
  242.     default:
  243.         return_error(gs_error_rangecheck);
  244.        }
  245.     old_dev = gdev_mem_device_for_bits(bits_per_pixel);
  246.     if ( old_dev == 0 )        /* no suitable device */
  247.         return_error(gs_error_rangecheck);
  248.     pcount = palette_size * 3;
  249.     /* Check to make sure the palette contains white and black, */
  250.     /* and, if it has any colors, the six primaries. */
  251.     if ( bits_per_pixel <= 8 )
  252.        {    const byte *p;
  253.         byte *q;
  254.         int primary_mask = 0;
  255.         int i;
  256.         has_color = 0;
  257.         for ( i = 0, p = colors, q = palette;
  258.               i < palette_size; i++, q += 3
  259.             )
  260.            {    int mask = 1;
  261.             switch ( bpp )
  262.                {
  263.             case 1:            /* gray */
  264.                 q[0] = q[1] = q[2] = *p++;
  265.                 break;
  266.             default:        /* bpp == 3, colored */
  267.                 q[0] = p[0], q[1] = p[1], q[2] = p[2];
  268.                 p += 3;
  269.                }
  270. #define shift_mask(b,n)\
  271.   switch ( b ) { case 0xff: mask <<= n; case 0: break; default: mask = 0; }
  272.             shift_mask(q[0], 4);
  273.             shift_mask(q[1], 2);
  274.             shift_mask(q[2], 1);
  275. #undef shift_mask
  276.             primary_mask |= mask;
  277.             if ( q[0] != q[1] || q[0] != q[2] )
  278.                 has_color = 1;
  279.            }
  280.         switch ( primary_mask )
  281.            {
  282.         case 129:        /* just black and white */
  283.             if ( has_color )    /* color but no primaries */
  284.                 return_error(gs_error_rangecheck);
  285.         case 255:        /* full color */
  286.             break;
  287.         default:
  288.             return_error(gs_error_rangecheck);
  289.            }
  290.        }
  291.     else
  292.         has_color = 1;
  293.     /*
  294.      * The initial transformation matrix must map 1 user unit to
  295.      * 1/72".  Let W and H be the width and height in pixels, and
  296.      * assume the initial matrix is of the form [A 0 0 B X Y].
  297.      * Then the size of the image in user units is (W/|A|,H/|B|),
  298.      * hence the size in inches is ((W/|A|)/72,(H/|B|)/72), so
  299.      * the number of pixels per inch is
  300.      * (W/((W/|A|)/72),H/((H/|B|)/72)), or (|A|*72,|B|*72).
  301.      * Similarly, if the initial matrix is [0 A B 0 X Y] for a 90
  302.      * or 270 degree rotation, the size of the image in user
  303.      * units is (W/|B|,H/|A|), so the pixels per inch are
  304.      * (|B|*72,|A|*72).  We forbid non-orthogonal transformation
  305.      * matrices.
  306.      */
  307.     if ( is_fzero2(pmat->xy, pmat->yx) )
  308.         x_pixels_per_unit = pmat->xx, y_pixels_per_unit = pmat->yy;
  309.     else if ( is_fzero2(pmat->xx, pmat->yy) )
  310.         x_pixels_per_unit = pmat->yx, y_pixels_per_unit = pmat->xy;
  311.     else
  312.         return_error(gs_error_undefinedresult);
  313.     /* All checks done, allocate the device. */
  314.     new_dev = (gx_device_memory *)(*mprocs->alloc)(1, old_dev->params_size, "gs_makeimagedevice(device)");
  315.     if ( new_dev == 0 ) return_error(gs_error_VMerror);
  316.     *new_dev = *old_dev;
  317.     new_dev->initial_matrix = *pmat;
  318.     new_dev->width = width;
  319.     new_dev->height = height;
  320.     new_dev->x_pixels_per_inch = fabs(x_pixels_per_unit) * 72;
  321.     new_dev->y_pixels_per_inch = fabs(y_pixels_per_unit) * 72;
  322.     if ( !has_color )
  323.         new_dev->color_info.max_rgb = 0,
  324.         new_dev->color_info.dither_rgb = 0;
  325.     dev_palette = (byte *)(*mprocs->alloc)(pcount, 1, "gs_makeimagedevice(palette)");
  326.     if ( dev_palette == 0 ) return_error(gs_error_VMerror);
  327.     new_dev->invert = (palette[0] | palette[1] | palette[2] ? -1 : 0);    /* bogus */
  328.     new_dev->palette_size = palette_size;
  329.     new_dev->palette = dev_palette;
  330.     memcpy(dev_palette, palette, pcount);
  331.     /* The bitmap will be allocated when the device is opened. */
  332.     new_dev->memory_procs = mprocs;
  333.     new_dev->is_open = 0;
  334.     *pnew_dev = (gx_device *)new_dev;
  335.     return 0;
  336. }
  337.  
  338. /* Set the device in the graphics state */
  339. int
  340. gs_setdevice(gs_state *pgs, gx_device *dev)
  341. {    register device *pdev = pgs->device;
  342.     int was_open = dev->is_open;
  343.     int code;
  344.     /* Initialize the device */
  345.     if ( !was_open )
  346.     {    gx_device_complete_procs(dev);
  347.         if ( gs_device_is_memory(dev) )
  348.         {    /* Set the target to the current device. */
  349.             gx_device *odev = pdev->info;
  350.             while ( odev != 0 && gs_device_is_memory(odev) )
  351.                 odev = ((gx_device_memory *)odev)->target;
  352.             ((gx_device_memory *)dev)->target = odev;
  353.         }
  354.         code = (*dev->procs->open_device)(dev);
  355.         if ( code < 0 ) return_error(code);
  356.         dev->is_open = 1;
  357.     }
  358.     /* Compute device white and black codes */
  359.     pdev->black = (*dev->procs->map_cmyk_color)(dev, 0, 0, 0, gx_max_color_value);
  360.     pdev->white = (*dev->procs->map_cmyk_color)(dev, 0, 0, 0, 0);
  361.     pdev->info = dev;
  362.     gx_set_cmap_procs(pgs);
  363.     if (    (code = gs_initmatrix(pgs)) < 0 ||
  364.         (code = gs_initclip(pgs)) < 0
  365.        )
  366.         return code;
  367.     if ( !was_open )
  368.         if ( (code = gs_erasepage(pgs)) < 0 )
  369.             return code;
  370.     return gx_remap_color(pgs);
  371. }
  372.  
  373. /* Select the null device.  This is just a convenience. */
  374. void
  375. gs_nulldevice(gs_state *pgs)
  376. {    gs_setdevice(pgs, (gx_device *)&gs_null_device);
  377. }
  378.  
  379. /* Close a device.  The client is responsible for ensuring that */
  380. /* this device is not current in any graphics state. */
  381. int
  382. gs_closedevice(gx_device *dev)
  383. {    int code = 0;
  384.     if ( dev->is_open )
  385.        {    code = (*dev->procs->close_device)(dev);
  386.         if ( code < 0 ) return_error(code);
  387.         dev->is_open = 0;
  388.        }
  389.     return code;
  390. }
  391.  
  392. /* Install enough of a null device to suppress graphics output */
  393. /* during the execution of stringwidth. */
  394. void
  395. gx_device_no_output(gs_state *pgs)
  396. {    pgs->device->info = (gx_device *)&gs_null_device;
  397. }
  398.  
  399. /* Just set the device without reinitializing. */
  400. /* (For internal use only.) */
  401. void
  402. gx_set_device_only(gs_state *pgs, gx_device *dev)
  403. {    pgs->device->info = dev;
  404. }
  405.  
  406. /* Compute the size of one scan line for a device, */
  407. /* with or without padding to a word boundary. */
  408. uint
  409. gx_device_raster(const gx_device *dev, int pad)
  410. {    ulong bits = (ulong)dev->width * dev->color_info.depth;
  411.     return (pad ?
  412.         (uint)((bits + (align_bitmap_mod * 8 - 1))
  413.              >> (log2_align_bitmap_mod + 3))
  414.             << log2_align_bitmap_mod :
  415.         (uint)((bits + 7) >> 3));
  416. }
  417.  
  418. /* Adjust the resolution for devices that only have a fixed set of */
  419. /* geometries, so that the apparent size in inches remains constant. */
  420. /* If fit=1, the resolution is adjusted so that the entire image fits; */
  421. /* if fit=0, one dimension fits, but the other one is clipped. */
  422. int
  423. gx_device_adjust_resolution(gx_device *dev,
  424.   int actual_width, int actual_height, int fit)
  425. {    double width_ratio = (double)actual_width / dev->width ;
  426.     double height_ratio = (double)actual_height / dev->height ;
  427.     double ratio =
  428.         (fit ? min(width_ratio, height_ratio) :
  429.          max(width_ratio, height_ratio));
  430.     dev->x_pixels_per_inch *= ratio;
  431.     dev->y_pixels_per_inch *= ratio;
  432.     dev->width = actual_width;
  433.     dev->height = actual_height;
  434.     return 0;
  435. }
  436.  
  437. /* ------ The null device ------ */
  438.  
  439. private int
  440. null_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  441.   gx_color_index color)
  442. {    return 0;
  443. }
  444. private int
  445. null_copy_mono(gx_device *dev, const byte *data,
  446.   int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  447.   gx_color_index zero, gx_color_index one)
  448. {    return 0;
  449. }
  450. private gx_xfont_procs *
  451. null_get_xfont_procs(gx_device *dev)
  452. {    gx_device *target = ((gx_device_null *)dev)->target;
  453.     return (target == 0 ? NULL :
  454.         (*target->procs->get_xfont_procs)(target));
  455. }
  456. private gx_device *
  457. null_get_xfont_device(gx_device *dev)
  458. {    gx_device *target = ((gx_device_null *)dev)->target;
  459.     return (target == 0 ? dev :
  460.         (*target->procs->get_xfont_device)(target));
  461. }
  462.  
  463. /* ------ Default device procedures ------ */
  464.  
  465. int
  466. gx_default_open_device(gx_device *dev)
  467. {    return 0;
  468. }
  469.  
  470. void
  471. gx_default_get_initial_matrix(register gx_device *dev, register gs_matrix *pmat)
  472. {    pmat->xx = dev->x_pixels_per_inch / 72.0;
  473.     pmat->xy = 0;
  474.     pmat->yx = 0;
  475.     pmat->yy = dev->y_pixels_per_inch / -72.0;
  476.     pmat->tx = 0;
  477.     pmat->ty = dev->height;    /****** WRONG for devices with ******/
  478.                 /****** arbitrary initial matrix ******/
  479. }
  480.  
  481. int
  482. gx_default_sync_output(gx_device *dev)
  483. {    return 0;
  484. }
  485.  
  486. int
  487. gx_default_output_page(gx_device *dev, int num_copies, int flush)
  488. {    return (*dev->procs->sync_output)(dev);
  489. }
  490.  
  491. int
  492. gx_default_close_device(gx_device *dev)
  493. {    return 0;
  494. }
  495.  
  496. int
  497. gx_default_copy_color(gx_device *dev, const byte *data,
  498.   int data_x, int raster, gx_bitmap_id id,
  499.   int x, int y, int width, int height)
  500. {    return (*dev->procs->copy_mono)(dev, data, data_x, raster, id,
  501.         x, y, width, height, (gx_color_index)0, (gx_color_index)1);
  502. }
  503.  
  504. int
  505. gx_default_get_bits(gx_device *dev, int y, byte *data, byte **actual_data)
  506. {    return -1;
  507. }
  508.  
  509. gx_xfont_procs *
  510. gx_default_get_xfont_procs(gx_device *dev)
  511. {    return NULL;
  512. }
  513.  
  514. gx_device *
  515. gx_default_get_xfont_device(gx_device *dev)
  516. {    return dev;
  517. }
  518.  
  519. /* Standard device properties */
  520.  
  521. private const gs_prop_item props_std[] = {
  522.         /* Following can be set, but will close and */
  523.         /* reopen the device if necessary. */
  524.     prop_def("HWResolution", prt_float_array),
  525.     prop_def("HWSize", prt_int_array),
  526.         /* Following cannot be set yet */
  527.     prop_def("InitialMatrix", prt_float_array),
  528.         /* Following cannot be set */
  529.     prop_def("Name", prt_string),
  530.         /* Slots for arrays */
  531.     prop_float, prop_float,
  532.     prop_int, prop_int,
  533.     prop_float, prop_float, prop_float, prop_float,
  534.       prop_float, prop_float
  535. };
  536.  
  537. /* Get the size of the device properties. */
  538. int
  539. gs_getdeviceprops_size(gx_device *dev)
  540. {    fill_proc(dev->procs, get_props);
  541.     return (*dev->procs->get_props)(dev, NULL);
  542. }
  543.  
  544. /* Get the device properties. */
  545. int
  546. gs_getdeviceprops(gx_device *dev, gs_prop_item *plist)
  547. {    fill_proc(dev->procs, get_props);
  548.     return (*dev->procs->get_props)(dev, plist);
  549. }
  550.  
  551. /* Get standard properties. */
  552. int
  553. gx_default_get_props(register gx_device *dev, register gs_prop_item *plist)
  554. {    if ( plist != 0 )
  555.        {    register gs_prop_item *pi;
  556.         gs_matrix mat;
  557.         memcpy(plist, props_std, sizeof(props_std));
  558.         plist[0].value.a.size = 2;
  559.         plist[1].value.a.size = 2;
  560.         plist[2].value.a.size = 6;
  561.         plist[3].value.a.p.s = (char *)dev->dname;
  562. #ifndef AMIGA
  563.         plist[3].value.a.size = -1;
  564. #else
  565.         plist[3].value.a.size = (unsigned short)-1;
  566. #endif
  567.         pi = &plist[4];
  568.             /* resolution array */
  569.         plist[0].value.a.p.v = pi;
  570.         pi[0].value.f = dev->x_pixels_per_inch;
  571.         pi[1].value.f = dev->y_pixels_per_inch;
  572.         pi += 2;
  573.             /* width/height array */
  574.         plist[1].value.a.p.v = pi;
  575.         pi[0].value.i = dev->width;
  576.         pi[1].value.i = dev->height;
  577.         pi += 2;
  578.             /* matrix */
  579.         plist[2].value.a.p.v = pi;
  580.  
  581.         fill_proc(dev->procs, get_initial_matrix);
  582.         (*dev->procs->get_initial_matrix)(dev, &mat);
  583.         pi[0].value.f = mat.xx;
  584.         pi[1].value.f = mat.xy;
  585.         pi[2].value.f = mat.yx;
  586.         pi[3].value.f = mat.yy;
  587.         pi[4].value.f = mat.tx;
  588.         pi[5].value.f = mat.ty;
  589.         pi += 6;
  590.        }
  591.     return sizeof(props_std) / sizeof(gs_prop_item);
  592. }
  593.  
  594. /* Set the device properties. */
  595. /* If the device was open and the put_props procedure closed it, */
  596. /* return 1; otherwise, return 0 or an error code as usual. */
  597. int
  598. gs_putdeviceprops(gx_device *dev, gs_prop_item *plist, int count)
  599. {    int was_open = dev->is_open;
  600.     int code;
  601.     fill_proc(dev->procs, put_props);
  602.     code = (*dev->procs->put_props)(dev, plist, count);
  603.     return (code < 0 ? code : was_open && !dev->is_open ? 1 : code);
  604. }
  605.  
  606. /* Set standard properties. */
  607. /* Note that setting the size or resolution closes the device. */
  608. int
  609. gx_default_put_props(gx_device *dev, gs_prop_item *plist, int count)
  610. {    gs_prop_item *known[2];
  611.     int code = 0;
  612.     gx_device temp_dev;
  613.     props_extract(plist, count, props_std, 2, known, 1);
  614.     temp_dev = *dev;
  615.     if ( known[1] != 0 )
  616.        {    if ( known[1]->value.a.size != 2 )
  617.             known[1]->status = pv_typecheck,
  618.             code = gs_error_typecheck;
  619.         else
  620.            {    gs_prop_item *ap = known[1]->value.a.p.v;
  621.             if ( ap[0].value.i <= 0 || ap[1].value.i <= 0 )
  622.                 known[1]->status = pv_rangecheck,
  623.                 code = gs_error_rangecheck;
  624. #define max_coord min(max_int, fixed2long(max_fixed))
  625.             else if ( ap[0].value.i > max_coord ||
  626.                  ap[1].value.i > max_coord
  627.                )
  628.                 known[1]->status = pv_limitcheck,
  629.                 code = gs_error_limitcheck;
  630. #undef max_coord
  631.             else
  632.                {    temp_dev.width = ap[0].value.i;
  633.                 temp_dev.height = ap[1].value.i;
  634.                }
  635.             if ( code == 0 ) code = 1;
  636.            }
  637.        }
  638.     if ( known[0] != 0 )
  639.        {    if ( known[0]->value.a.size != 2 )
  640.             known[0]->status = pv_typecheck,
  641.             code = gs_error_typecheck;
  642.         else
  643.            {    gs_prop_item *ap = known[0]->value.a.p.v;
  644.             if ( ap[0].value.f <= 0 || ap[1].value.f <= 0 )
  645.                 known[0]->status = pv_rangecheck,
  646.                 code = gs_error_rangecheck;
  647.             else
  648.                {    temp_dev.x_pixels_per_inch = ap[0].value.f;
  649.                 temp_dev.y_pixels_per_inch = ap[1].value.f;
  650.                }
  651.             if ( code == 0 ) code = 1;
  652.            }
  653.        }
  654.     if ( code < 0 )
  655.         return_error(code);
  656.     /* Close the device; gs_putdeviceprops will reopen it. */
  657.     if ( dev->is_open && code )
  658.     {    int ccode = gs_closedevice(dev);
  659.         if ( ccode < 0 ) return ccode;
  660.     }
  661.     dev->x_pixels_per_inch = temp_dev.x_pixels_per_inch;
  662.     dev->y_pixels_per_inch = temp_dev.y_pixels_per_inch;
  663.     dev->width = temp_dev.width;
  664.     dev->height = temp_dev.height;
  665.     return code;
  666. }
  667.